from flask import Blueprint, Response, request
from relex.model import db, LexDoc
from relex.generators.lexdoc import TGLexDoc
from relex.parsers.lexdoc import FPLexDoc

lexdoc_blue = Blueprint('lexdoc', __name__)

@lexdoc_blue.route('/api/1/lexdoc', methods=['POST'])
def newLexDoc():
	doc = FPLexDoc.parse(request.form)
	db.session.add(doc)
	db.session.commit()
	return Response(unicode(doc.id), status=201, mimetype='text/plain')

@lexdoc_blue.route('/api/1/lexdoc', methods=['GET'])
def getLexDoc():
	docs = LexDoc.query.all()
	generator = TGLexDoc(docs)
	return Response(generator.generate(), mimetype='text/plain')
